<template>
    <div class="login_container">
        <div class="login_box" ref="diva">
            <div class="avatar">
                <img class="avatar_img" src="../assets/logo.png" alt="">
            </div>
            <!-- 表单 -->
            <el-form label-width="0px" class="login_form" ref="loginFormRef" :model="formData" :rules="loginFormRules">
                <el-form-item prop="username">
                    <el-input prefix-icon="iconfont icon-01" v-model="formData.username"></el-input>
                </el-form-item>
                <el-form-item label="" prop="password">
                    <el-input type="password" prefix-icon="iconfont icon-icon-test" v-model="formData.password"></el-input>
                </el-form-item>
                <el-form-item class="buttons" label="">
                    <el-button type="primary" @click="loginIn">登录</el-button>
                    <el-button type="info" @click="resetLoginForm">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      // 绑定的表单数据对象
      formData: {
        username: 'admin',
        password: '123456'
      },
      // 表单验证规则对象
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户', trigger: 'blur' },
          { min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur' }
        //   { required: true, message: '请输入用户', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetLoginForm: function () {
    //   this=》当前组件vue实例
      console.log(this)
      this.$refs.loginFormRef.resetFields()
    },
    loginIn: function () {
      // 校验表格的所有表单项(相当于调用了所有表单项校验规则) 全通过在发送登陆请求 否则取消请求
      this.$refs.loginFormRef.validate(async isSuccess => {
        if (!isSuccess) {

        } else {
          var { data: res } = await this.$http.post('login', this.formData)
          if (res.meta.status === 200) {
            this.$message.success('登陆成功')
            // 每个前端网站都有一个session Storage 还有Local Storage
            window.sessionStorage.setItem('token', res.data.token)
            console.log(res)
            this.$router.push('/home')
          } else {
            this.$message.error('登陆失败')
          }
        }
      })
    }
  }
}
</script>

<style scoped>

.login_container{
    background-color: #2b4b6b;
    height: 100%;
}
.login_box{
    width: 500px;
    height: 300px;
    background-color: #fff;
    position: absolute;
    left: 50%;
    top: 50%;
    border-radius: 5px;
    transform: translate(-50%,-50%);
}
.avatar{
    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    background-color: #fff;
    padding: 10px;
    position: absolute;
    left: 50%;
    top: -50%;
    transform: translate(-50%,50%);
    box-shadow: 0px 0px #ddd 10px;
}
.avatar .avatar_img{
    /* border-radius: 50%; */
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
}
.login_form{
    position: absolute;
    width: 100%;
    padding: 0 50px;
    box-sizing: border-box;
    bottom: 0px;
}
.buttons{
    display: flex;
    justify-content: flex-end;
}

</style>
